package com.travel.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.xy.XYDataset;

import com.travel.core.DaoFactory;
import com.travel.dao.ISpotDao;
import com.travel.service.DrawBar;
import com.travel.service.DrawLine;
import com.travel.service.DrawPie;

public class JfreeTour extends HttpServlet {

    private static final long serialVersionUID = 994077646128526088L;

    private static final ISpotDao SPOT_DAO = DaoFactory.getDao(ISpotDao.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("charset=utf-8");
        OutputStream out = response.getOutputStream();
        String type = request.getParameter("type");
        String spot = request.getParameter("spot").trim();
        String year1 = request.getParameter("year1");
        String year2 = request.getParameter("year2");
        String year3 = request.getParameter("year3");
        List<String> years = new ArrayList<String>();
        if (!"".equals(year1)) {
            years.add(year1);
        }
        if (!"".equals(year2)) {
            years.add(year2);
        }
        if (!"".equals(year3)) {
            years.add(year3);
        }
        String zfValue = request.getParameter("zf");
        String dayTime = request.getParameter("daytime");
        String ym = request.getParameter("ym");
        System.out.println("type=" + type);
        System.out.println("spot=" + spot);
        System.out.println(years);
        if ("01".equals(zfValue)) {// 查询的是某一天的客流量
            StringBuffer sb = new StringBuffer();
            sb.append(spot + "景区<span color='red'>" + dayTime + "</span>客流量统计数据为<span color='red'>");
            sb.append(SPOT_DAO.getStatisticsNumberByCityAndDate(spot, 1, dayTime));
            sb.append("</span>");
            out.write(sb.toString().getBytes("utf-8"));
        } else if ("02".equals(zfValue)) {// 查询的是本周的客流量
            StringBuffer sb = new StringBuffer();
            sb.append(spot + "景区本周<span color='red'>" + "0000-00-00" + "</span>客流量统计数据为<span color='red'>");
            sb.append(SPOT_DAO.getStatisticsNumberByCityAndDate(spot, 1, null));
            sb.append("</span>");
            out.write(sb.toString().getBytes("utf-8"));
        } else if ("03".equals(zfValue)) {// 查询的是某一月的客流量
            StringBuffer sb = new StringBuffer();
            sb.append(spot + "景区<span color='red'>" + ym.substring(0, 4) + "年" + ym.substring(5) + "月"
                    + "</span>客流量统计数据为<span color='red'>");
            sb.append(SPOT_DAO.getStatisticsNumberByCityAndDate(spot, 1, ym));
            sb.append("</span>");
            out.write(sb.toString().getBytes("utf-8"));
        } else {
            // 判断时间的长度
            JFreeChart chart = null;
            if (!years.isEmpty()) {
                if (type.equals("line")) {
                    chart = drawPlotLine(out, spot, years);
                }
                if (type.equals("bar")) {
                    chart = drawPlotBar(out, spot, years);
                }
                if (type.equals("pie")) {
                    chart = drawPlotPie(out, spot, years);
                }
            }
            StandardEntityCollection sec = new StandardEntityCollection();
            ChartRenderingInfo info = new ChartRenderingInfo(sec);
            PrintWriter w = new PrintWriter(out);
            ChartUtils.writeImageMap(w, "img", info, false);
            response.setContentType("image/jpeg ");
            ChartUtils.writeChartAsJPEG(out, chart, 400, 300);
            out.flush();
            out.close();
        }
    }

    private JFreeChart drawPlotPie(OutputStream out, String spot, List<String> years) {
        JFreeChart chart = new DrawPie(spot, spot, years, 1).yearSpotPie();
        return chart;
    }

    private JFreeChart drawPlotBar(OutputStream out, String spot, List<String> years) {
        DrawBar spotbar = null;
        CategoryDataset categorydatasets = null;
        JFreeChart chart = null;
        spotbar = new DrawBar(spot, spot, years, 1);
        categorydatasets = spotbar.getDataSet(years.size());
        chart = spotbar.feng(categorydatasets);
        return chart;
    }

    private JFreeChart drawPlotLine(OutputStream out, String spot, List<String> years) {
        DrawLine spotline = null;
        XYDataset xydatasets = null;
        JFreeChart chart = null;
        spotline = new DrawLine(spot, spot, years, 1);
        xydatasets = spotline.createDatasets(years.size());
        chart = spotline.yearSpotLine(xydatasets);
        return chart;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
